package com.android.love.ui;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.android.love.R;
import com.android.love.adapter.ClassAdapter;
import com.android.love.util.ActivityUtil;
import com.android.love.util.DeviceUtil;
import com.android.love.util.LogUtil;
import com.github.kevinsawicki.wishlist.ViewFinder;
import com.github.kevinsawicki.wishlist.ViewUtils;

public class ClasssifyActivity extends BasePageActivity {

	private ViewFinder cFinder;
	private ListView listView;
	private EditText et_search;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mApplication.setDefaultIndex(CLASSIFY);
		View classView = inflater.inflate(R.layout.activity_class, null);
		setContentView(classView);
		cFinder = new ViewFinder(classView);
		// 隐藏软键盘
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		process();
		setListener();
	}

	@Override
	protected void process() {
		super.process();
		listView = cFinder.find(R.id.lv_classify);
		ClassAdapter adapter = new ClassAdapter(context);
		View searchView = inflater.inflate(R.layout.search_item, null);
		et_search = (EditText) searchView.findViewById(R.id.et_search);
		cFinder.setDrawable(R.id.iv_top_meinv, R.drawable.meinv);
		cFinder.setText(R.id.top_title, "分类");
		listView.addHeaderView(searchView);
		listView.setAdapter(adapter);
		ViewUtils.setGone(cFinder.find(R.id.iv_kefu), false);
		ViewUtils.setGone(cFinder.find(R.id.iv_shuaxin), true);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				LogUtil.i(TAG, "pos---"+position);
				Intent intent = new Intent(context,
						ClassifyDetailActivity.class);
				intent.putExtra("type", position);
				startActivity(intent);

			}
		});
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.iv_kefu:
			showPopupWindow(cFinder.find(R.id.iv_kefu), 1);
			break;

		default:
			break;
		}
	}

	@Override
	protected void onResume() {
		mApplication.setDefaultIndex(CLASSIFY);
		super.onResume();
	}

	@Override
	protected void setListener() {
		super.setListener();
		cFinder.find(R.id.iv_kefu).setOnClickListener(this);
		et_search.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_SEARCH) {
					// 搜索
					Intent intent = new Intent(context,
							ClassifyDetailActivity.class);
					intent.putExtra("type", -1);
					intent.putExtra("search", true);
					intent.putExtra("keywords", et_search.getText().toString()
							.trim());
					startActivity(intent);
				}
				return false;
			}
		});
	}

}
